package com.heyun.controller;

import com.heyun.constant.Constants;
import com.heyun.entity.vo.UserVO;
import com.heyun.result.Result;
import com.heyun.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Map;
@Slf4j
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    UserService userService;
    @Autowired
    private SimpMessagingTemplate messagingTemplate;

    /**
     * 初始化用户信息
     * @return
     */
    @GetMapping("/init")
    Result<UserVO> initData(){

        UserVO userVO = userService.initData();

        if(userVO.getUserId().length() < 2 ) {
            return Result.error(Constants.USER_INFO_FAILURE);
        }
        else {
            log.info(Constants.USER_INFO_SUCCESS+userVO.toString());
//            messagingTemplate.convertAndSend("/topic/signIn", "签到已发布，请及时签到！");
            return Result.success(userVO);
        }
    }


    /**
     * 修改用户信息
     * @param email
     * @return
     */
    @PutMapping("/update")
    Result updateInfo(String email) {
        Boolean res = userService.updateInfo(email);
        if (res) {
            log.info(Constants.UPDATE_USER_INFO_SUCCESS + email);
            return Result.success();
        }else return Result.error(Constants.UPDATE_USER_INFO_FAILURE);
    }

    /**
     * 获取所有用户信息
     * @return
     */
    @GetMapping("/getAllUser")
    Result getAllUsers() {
        UserVO[] all = userService.getAll();
        if (all.length > 0) {
            log.info(Constants.CLASS_SUCCESS + Arrays.toString(all));
            return Result.success(all);
        }
        return Result.error(Constants.CLASS_FAILURE);
    }


    @GetMapping("/getLoginLog")
    Result getLog(@RequestParam String userId){
        return Result.success(userService.getBehavior(userId));
    }

    @PutMapping("/updateUserInfo")
    Result updateUserInfo(@RequestBody UserVO userVO){
        return Result.success(userService.updateUserInfo(userVO));
    }

    @PostMapping("/registerUser")
    Result register(@RequestBody UserVO userVO){
        return Result.success(userService.addUser(userVO));
    }
}
